package com.ruoyi.xxljob.admin.controller;

import com.ruoyi.xxljob.admin.service.impl.AdminBizImpl;
import com.ruoyi.xxljob.core.biz.model.HandleCallbackParam;
import com.ruoyi.xxljob.core.biz.model.RegistryParam;
import com.ruoyi.xxljob.core.biz.model.ReturnT;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by xuxueli on 17/5/10.
 */
@RestController
public class JobApiController {

    @Resource
    private AdminBizImpl adminBiz;

    @PostMapping("/api/callback")
    public ReturnT<String> callback(@RequestBody List<HandleCallbackParam> callbackParamList) {
        return adminBiz.callback(callbackParamList);
    }

    @PostMapping("/api/registry")
    public ReturnT<String> registry(@RequestBody RegistryParam registryParam) {
        return adminBiz.registry(registryParam);
    }

    @PostMapping("/api/registryRemove")
    public ReturnT<String> registryRemove(@RequestBody RegistryParam registryParam) {
        return adminBiz.registryRemove(registryParam);
    }

//    /**
//     * api
//     *
//     * @param uri
//     * @param data
//     * @return
//     */
//    @RequestMapping("/{uri}")
//    @ResponseBody
//    @PermissionLimit(limit = false)
//    public ReturnT<String> api(HttpServletRequest request, @PathVariable("uri") String uri, @RequestBody(required = false) String data) {
//
//        // valid
//        if (!"POST".equalsIgnoreCase(request.getMethod())) {
//            return new ReturnT<String>(ReturnT.FAIL_CODE, "invalid request, HttpMethod not support.");
//        }
//        if (uri == null || uri.trim().length() == 0) {
//            return new ReturnT<String>(ReturnT.FAIL_CODE, "invalid request, uri-mapping empty.");
//        }
//        if (XxlJobAdminConfig.getAdminConfig().getAccessToken() != null
//                && XxlJobAdminConfig.getAdminConfig().getAccessToken().trim().length() > 0
//                && !XxlJobAdminConfig.getAdminConfig().getAccessToken().equals(request.getHeader(XxlJobRemotingUtil.XXL_JOB_ACCESS_TOKEN))) {
//            return new ReturnT<String>(ReturnT.FAIL_CODE, "The access token is wrong.");
//        }
//
//        // services mapping
//        if ("callback".equals(uri)) {
//            List<HandleCallbackParam> callbackParamList = GsonTool.fromJson(data, List.class, HandleCallbackParam.class);
//            return adminBiz.callback(callbackParamList);
//        } else if ("registry".equals(uri)) {
//            RegistryParam registryParam = GsonTool.fromJson(data, RegistryParam.class);
//            return adminBiz.registry(registryParam);
//        } else if ("registryRemove".equals(uri)) {
//            RegistryParam registryParam = GsonTool.fromJson(data, RegistryParam.class);
//            return adminBiz.registryRemove(registryParam);
//        } else {
//            return new ReturnT<String>(ReturnT.FAIL_CODE, "invalid request, uri-mapping(" + uri + ") not found.");
//        }
//
//    }

}
